using System;
using UnityEngine;
using UnityEngine.UI;

namespace Voodoo.Sauce.Debugger
{
	public class DebuggerPopupScreen : MonoBehaviour
	{
		private const string PREFIX_IDFV = "Idfv: ";

		private const string PREFIX_IDFA = "Idfa: ";

		[SerializeField]
		private Text title;

		[SerializeField]
		private Text message;

		[SerializeField]
		private IdSectionDebugger idfvSectionDebugger;

		[SerializeField]
		private IdSectionDebugger idfaSectionDebugger;

		[SerializeField]
		private Button cancelButton;

		[SerializeField]
		private Button confirmButton;

		private DebuggerPopupConfig _currentConfig;

		private Action _closeCallback;

		private void Awake()
		{
			confirmButton.onClick.AddListener(OnConfirmButtonClicked);
			cancelButton.onClick.AddListener(OnCancelButtonClicked);
		}

		private void OnDestroy()
		{
			confirmButton.onClick.RemoveAllListeners();
			cancelButton.onClick.RemoveAllListeners();
		}

		public void Show(DebuggerPopupConfig config, Action closeCallback = null)
		{
			Open();
			_closeCallback = closeCallback;
			_currentConfig = config;
			title.text = _currentConfig.title;
			message.text = _currentConfig.message;
			confirmButton.gameObject.SetActive(_currentConfig.showConfirmButton);
			cancelButton.gameObject.SetActive(_currentConfig.showCancelButton);
			cancelButton.GetComponentInChildren<Text>().text = config.cancelText;
			confirmButton.GetComponentInChildren<Text>().text = config.confirmText;
			idfvSectionDebugger.UpdateDisplay(_currentConfig.showIdfv, "Idfv: ", _currentConfig.idfv);
			idfaSectionDebugger.UpdateDisplay(_currentConfig.showIdfa, "Idfa: ", _currentConfig.idfa);
		}

		private void OnCancelButtonClicked()
		{
			Close();
			_currentConfig.cancelCallback?.Invoke();
		}

		private void OnConfirmButtonClicked()
		{
			Close();
			_currentConfig.confirmCallback?.Invoke();
		}

		private void Open()
		{
			base.gameObject.SetActive(value: true);
		}

		private void Close()
		{
			_closeCallback?.Invoke();
			base.gameObject.SetActive(value: false);
		}
	}
}
